<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\WordModel;

class Word extends Base
{
    // 绘本列表
    public function index()
    {
        if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = ['is_del' => '0'];
            if (!empty($param['searchText'])) {
                $where['title'] = ['like', '%' . $param['searchText'] . '%'];
            }
            $word = new wordModel();
            $selectResult = $word->getwordsByWhere($where, $offset, $limit);
            foreach($selectResult as $key=>$vo){
                $pic = json_decode($vo['pic']);
                $str = '';
                foreach($pic as $val){
                    $str .= '<img src="' .$val. '" height="40px" style="margin-right:5px">';
                }
                $selectResult[$key]['pic'] = $str;
                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));
                $selectResult[$key]['addtime'] = $vo['addtime'];
                $selectResult[$key]['is_put'] = ($vo['is_put'] == '0')?'下架':'上架';
            }
            $return['total'] = $word->getAllwords($where);  // 总数据
            $return['rows'] = $selectResult;
            return json($return);
        }

        return $this->fetch();
    }

    // 添加文章
    public function wordadd()
    {
        if(request()->isPost()){
            $param = input('post.');
            unset($param['file']);
            if(empty($param['pic'])){
                return json(msg('','','图片不为空'));
            }
            $param['pic'] = json_encode($param['pic']);
            $param['addtime'] = date('Y-m-d H:i:s');
            if(!empty($param['translate'])){
                $param['translate'] = json_encode($param['translate']);
            }else{
                $param['translate'] = '';
            }
            $word = new wordModel();
            $flag = $word->addword($param,url('word/index'));

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        return $this->fetch();
    }

    public function wordedit()
    {
        $word = new wordModel();
        if(request()->isPost()){

            $param = input('post.');
            unset($param['file']);
            $param['pic'] = json_encode($param['pic']);
            $param['translate'] = json_encode($param['translate']);
            $flag = $word->editword($param,['id' => $param['id']],url('word/index'));

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $id = input('param.id');
        $word_data = $word->getOneword(['id' => $id],'*');
        if($word_data['pic'] == ''){
            $pic_str = '';
        }else{
            $pic = json_decode($word_data['pic']);
            $pic_str = '';
            foreach($pic as $key=>$val){
                $pic_str .=  '<dd class="item_img" id="pic'.$key.'"><div class="operate"><i onclick=upload_img_del("pic'.$key.'","'.$val.'") class="close layui-icon"></i></div><img src="'.$val.'" class="img" ><input type="hidden" name="pic[]" value="'.$val.'" /></dd>';
            }
        }
        $word_data['pic'] = $pic_str;
        $word_data['translate'] = json_decode($word_data['translate']);
        $this->assign([
            'word' => $word_data
        ]);
        return $this->fetch();
    }

    public function wordDel()
    {
        $id = input('param.id');
        $word = new wordModel();
        $flag = $word->delword($id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }
    function uploadimg_del(){
        $path = input('param.pic');
        unset($path);
    }
    public function wordisput()
    {
        $id = input('param.id');
        $is_put = input('param.is_put');
        $word = new wordModel();
        $flag = $word->word_is_put(['is_put' => $is_put],['id' => $id]);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }

    // 上传缩略图
    public function uploadimg()
    {
        if(request()->isAjax()){
            $file = request()->file('file');
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload'.DS.'image');
            if($info){
                $src =  '/upload'.'/image' . '/' . date('Ymd') . '/' . $info->getFilename();
                return json(msg(0, ['src' => $src], ''));
            }else{
                // 上传失败获取错误信息
                return json(msg(-1, '', $file->getError()));
            }
        }
    }
    // 音频文件
    public function uploadmusic()
    {
        if(request()->isAjax()){
            $file = request()->file('file');
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload'.DS.'music');
            if($info){
                $src =  '/upload/music' . '/' . date('Ymd') . '/' . $info->getFilename();
                return json(msg(0, ['src' => $src], ''));
            }else{
                // 上传失败获取错误信息
                return json(msg(-1, '', $file->getError()));
            }
        }
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        $word = new wordModel();
        $is_put = $word->getOnefield('is_put',['id' => $id]);
        $return_data = [
            '编辑' => [
                'auth' => 'word/wordedit',
                'href' => url('word/wordedit', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '删除' => [
                'auth' => 'word/worddel',
                'href' => "javascript:wordDel(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ],
        ];
        if(!$is_put){
            $return_data['上架'] =  [
                'auth' => 'word/wordedit',
                'href' => "javascript:wordisput(".$id.",1".")",
                'btnStyle' => 'primary',
                'icon' => ''
            ];
        }else{
            $return_data['下架'] =  [
                'auth' => 'word/wordedit',
                'href' => "javascript:wordisput(".$id.",0".")",
                'btnStyle' => 'primary',
                'icon' => ''
            ];
        }
        return $return_data;

    }
     // 绘本列表
    public function wordsonlist()
    {
        $pid = input('param.id');
        if(request()->isAjax()){
            $param = input('param.'); 
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = ['is_del' => '0','pid' => $param['pid']];
            if (!empty($param['searchText'])) {
                $where['title'] = ['like', '%' . $param['searchText'] . '%'];
            }

            $word = new wordModel();
            $selectResult = $word->getwordsByWhere($where, $offset, $limit);
            // echo $word->getLastSql();
            // var_dump($selectResult);
            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['pic'] = '<img src="' . $vo['pic'] . '" width="40px" height="40px">';
                $selectResult[$key]['audio'] = '<audio src="'.$vo['audio'].'" controls="controls"> </audio>';
                $selectResult[$key]['operate'] = showOperate($this->makesonButton($vo['id']));
                $selectResult[$key]['addtime'] = $vo['addtime'];
            }

            $return['total'] = $word->getAllwords($where);  // 总数据
            $return['rows'] = $selectResult;
            return json($return);
    }
        $this->assign('pid',$pid);
        return $this->fetch();
    }
     // 添加文章
    public function wordsonadd()
    {
        $pid = input('param.pid');
        if(request()->isPost()){
            $param = input('post.');
            unset($param['file']);
            $param['addtime'] = date('Y-m-d H:i:s');
            $word = new wordModel();
            $flag = $word->addword($param,url('word/wordsonlist',['id' => $param['pid']]));
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $this->assign('pid',$pid);
        return $this->fetch();
    }
    public function wordsonedit()
    {
        $word = new wordModel();
        if(request()->isPost()){
            $param = input('post.');
            unset($param['file']);
            $flag = $word->editword($param,['id' => $param['id']],url('word/wordsonlist',['id' => $param['pid']]));
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $id = input('param.id');
        $this->assign([
            'word' => $word->getOneword(['id' => $id],'*')
        ]);
        return $this->fetch();
    }
    public function wordsonDel()
    {
        $id = input('param.id');
        $word = new wordModel();
        $flag = $word->delword($id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }
     /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makesonButton($id)
    {
        $word = new wordModel();
        $return_data = [
            '编辑' => [
                'auth' => 'word/wordedit',
                'href' => url('word/wordsonedit', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '删除' => [
                'auth' => 'word/worddel',
                'href' => "javascript:wordDel(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ],
        ];
        return $return_data;

    }
}
